define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classroom/course_msg/index' + location.search,
                    add_url: 'classroom/course_msg/add',
                    edit_url: 'classroom/course_msg/edit',
                    del_url: 'classroom/course_msg/del',
                    multi_url: 'classroom/course_msg/multi',
                    import_url: 'classroom/course_msg/import',
                    table: 'classroom_course_msg',
                }
            });

            var table = $("#table");

            // 审核
            $(document).on("click",".audit_msg",function () {
                let ids = $(this).attr('data-id');
                let status = $(this).attr('data-status');
                $.ajax({
                    url: "classroom/course_msg/audit",
                    data: {ids,status},
                    success: function (res) {
                        if(res.code){
                            Fast.api.toastr.success(res.msg);
                            table.bootstrapTable('refresh');
                        } else {
                            Fast.api.toastr.error(res.msg);
                        }
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable: true},
                        {field: 'user_id', title: __('User_id'),visible:false,operate: false},
                        {field: 'course_id', title: __('Course_id'),visible:false,operate: false},
                        {field: 'lesson.title', title: __('Lesson.title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'lesson_id', title: __('Lesson_id'),visible:false,operate: false},
                        {field: 'content', title: __('Content'), operate: 'LIKE'},
                        {
                            field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')},sortable: true,
                            formatter: function (value, row, index) {
                                if(value === 0) {
                                    return `<button data-id="${row.id}" data-status="1" class="btn btn-success btn-xs audit_msg"><i class="fa fa-check"></i> 设为通过</button> <button data-id="${row.id}" data-status="2" class="btn btn-danger btn-xs audit_msg"><i class="fa fa-close"></i> 拒绝通过</button>`;
                                }else if(value === 1) {
                                    return `<span class="text-success">已通过</span>`;
                                }else if(value === 2) {
                                    return `<span class="text-danger">已拒绝</span>`;
                                }
                            }
                        },
                        {field: 'user.nickname', title: __('User_id'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'second', title: __('Second'),visible:false,operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
